package model.readers;



import java.util.HashMap;
import java.util.Hashtable;

import exceptions.ReaderNotFoundException;


/**
 * Factory for MetroModelReader
 * 
 * @author Ilya Shurigyn (honorik@gmail.com)
 */
public class MetroModelReaderFactory {
	/**
	 * now this method simply returns SaxReader instance without any file
	 * pre-analyzing (may be implemented in future)
	 * 
	 * @return MetroModelReader implementation instance
	 * @throws ModelNotAssignedBuilderException
	 */
	private static Hashtable<String, MetroReader> formats = new Hashtable<String, MetroReader>();
	
	public static MetroReader getReader(String path) throws ReaderNotFoundException {
		
		MetroReader reader = formats.get(getExtension(path.toLowerCase()));				
		if(reader == null) throw new ReaderNotFoundException();		
		return reader;
	}
	
	private static String getExtension(String path) {		
		
		return path.substring(path.lastIndexOf('.')+1);
	}

	/**
	 * associate file extension with the reader
	 * @param fileExtension file extension. "exe" or "xml" for example
	 * @param reader reader that can read this format
	 */
	static void associate(String shortFormatDescription, String fileExtension, MetroReader reader) {
		formats.put(fileExtension.toLowerCase(), reader);
	}

}
